#!/bin/bash
# Grep the reported errors from the files in arguments.
# Sum up the number of the reported errors.

total_file=0
total_ok_file=0
error_cnt=0
ml_cnt=0
error_file=0 # and error_files
error_ok_file=0
noerr_file=0 # and noerr_files
noerr_ok_file=0
empty_file=0 # and empty_files
empty_ok_file=0
nonex_file=0 # and nonex_files

for file in $*; do
  flag=${file##*-} # of the form *-flag.error.txt
  flag=${flag%.error.txt}

  total_file=$(expr ${total_file} + 1)
  if [ "${flag}" = "ok" ]; then total_ok_file=$(expr ${total_ok_file} + 1); fi

  if [ -s ${file} ]; then
    error=$(cat ${file} | grep "SUMMARY: AddressSanitizer:" | wc -l)
    ml=$(cat ${file} | grep "LeakSanitizer: detected memory leak" | wc -l)
    error_cnt=$(expr ${error_cnt} + ${error})
    ml_cnt=$(expr ${ml_cnt} + ${ml})
    if [ "${error}" != "0" ]; then
      error_file=$(expr ${error_file} + 1)
      error_files="${error_files} ${file}"
      if [ "${flag}" = "ok" ]; then error_ok_file=$(expr ${error_ok_file} + 1); fi
    else
      noerr_file=$(expr ${noerr_file} + 1)
      noerr_files="${noerr_files} ${file}"
      if [ "${flag}" = "ok" ]; then noerr_ok_file=$(expr ${noerr_ok_file} + 1); fi
    fi
  elif [ -f ${file} ]; then
    empty_file=$(expr ${empty_file} + 1)
    empty_files="${empty_files} ${file}"
    if [ "${flag}" = "ok" ]; then empty_ok_file=$(expr ${empty_ok_file} + 1); fi
  else
    nonex_file=$(expr ${nonex_file} + 1)
    nonex_files="${nonex_files} ${file}"
  fi
done

if [ "${error_file}" != "0" ]; then
echo "========== Programs with errors detected are as follows."
for file in ${error_files}; do
  echo ${file%.error.txt}
done
fi

if [ "${noerr_file}" != "0" ]; then
echo "========== Programs WITHOUT errors detected are as follows."
for file in ${noerr_files}; do
  echo ${file%.error.txt}
done
fi

if [ "${empty_file}" != "0" ]; then
echo "========== Please manually run the following" ${empty_file} "programs to check for memory errors, as their output files are empty."
for file in ${empty_files}; do
  echo ${file%.error.txt}
done
fi

if [ "${nonex_file}" != "0" ]; then
echo "========== The following output files do not exist."
for file in ${nonex_files}; do
  echo ${file}
done
fi

msg="${error_cnt} memory errors"
if [ "${ml_cnt}" != "0" ]; then msg=${msg}" (ML=${ml_cnt})"; fi
echo "========================================"
echo "There are" ${total_file} "programs in total (of which" ${total_ok_file} "are labeled as OK). ${msg} have been detected in the output files generated by the above" ${error_file} "programs (of which" ${error_ok_file} "are labeled as OK). No error has been detected in" ${noerr_file} "programs (of which" ${noerr_ok_file} "are labeled as OK). The remaining" ${empty_file} "programs (of which" ${empty_ok_file} "are labeled as OK) have produced empty output files (subject to manual run), possibly due to segmentation faults, special command-line options, environment variables or input files."

# Write to the specified file.
if [ -n "${BENCHMARK_TABLE_FILE}" ]; then
  echo -n "${total_file},$(expr ${total_file} - ${total_ok_file}),${total_ok_file},${error_file},${error_ok_file},$(expr ${noerr_file} + ${empty_file}),$(expr ${noerr_ok_file} + ${empty_ok_file})," >> ${BENCHMARK_TABLE_FILE}
fi
